﻿namespace Chain.Common
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Web;

    public class ImagePlus
    {
        public static Image BytToImg(byte[] byt)
        {
            if (byt.Length > 0)
            {
                MemoryStream stream = new MemoryStream(byt);
                return Image.FromStream(stream);
            }
            return null;
        }

        public static byte[] ImgToByt(Image img)
        {
            if (img == null)
            {
                return null;
            }
            MemoryStream stream = new MemoryStream();
            img.Save(stream, ImageFormat.Jpeg);
            return stream.GetBuffer();
        }

        public static string SaveFile(HttpPostedFile myFile, string savePath, string allowType)
        {
            string fileName = myFile.FileName;
            string str2 = fileName.Substring(fileName.LastIndexOf(".") + 1);
            Random random = new Random();
            string str3 = DateTime.Now.Ticks.ToString() + random.Next(100).ToString();
            int num = 0x400000;
            if (myFile.ContentLength >= num)
            {
                return "ERROR:上传的文件不能超过4M";
            }
            if (allowType.IndexOf(str2) == -1)
            {
                return "ERROR:不允许上传此类文件，请与系统管理员联系";
            }
            string path = savePath;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = path + str3 + "." + str2;
            myFile.SaveAs(path);
            return ("TRUE:[FILENAME]" + fileName + "[SAVEPATH]" + path);
        }

        public static void UpLoadExcel(HttpPostedFile myFile, string sSavePath)
        {
            string filename = HttpContext.Current.Server.MapPath("/WebUI/AppData/upload/member/member.xls");
            myFile.SaveAs(filename);
        }

        public static string UpLoadImage(HttpPostedFile myFile, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
        {
            string str = "";
            string str2 = "";
            if (myFile == null)
            {
                return "没有选择图片";
            }
            int contentLength = myFile.ContentLength;
            if (contentLength == 0)
            {
                return "没有选择上传图片";
            }
            string str3 = Path.GetExtension(myFile.FileName).ToLower();
            if ((((str3 != ".jpg") && (str3 != ".jpge")) && ((str3 != ".gif") && (str3 != ".bmp"))) && (str3 != ".png"))
            {
                return "图片格式不正确";
            }
            byte[] buffer = new byte[contentLength];
            myFile.InputStream.Read(buffer, 0, contentLength);
            str2 = DateTime.Now.ToString("ddHHmmssffff") + str3;
            FileStream stream = new FileStream(HttpContext.Current.Server.MapPath(sSavePath + str2), FileMode.Create, FileAccess.Write);
            stream.Write(buffer, 0, buffer.Length);
            stream.Close();
            try
            {
                using (Image image = Image.FromFile(HttpContext.Current.Server.MapPath(sSavePath + str2)))
                {
                    int num4;
                    int num5;
                    int width = image.Width;
                    int height = image.Height;
                    if ((width / height) <= (intThumbWidth / intThumbHeight))
                    {
                        num4 = intThumbWidth;
                        num5 = (intThumbWidth * height) / width;
                    }
                    else
                    {
                        num4 = (intThumbHeight * width) / height;
                        num5 = intThumbHeight;
                    }
                    str = sThumbExtension + str2;
                    string filename = HttpContext.Current.Server.MapPath(sSavePath) + str;
                    using (Image image2 = new Bitmap(num4, num5))
                    {
                        using (Graphics graphics = Graphics.FromImage(image2))
                        {
                            graphics.InterpolationMode = InterpolationMode.High;
                            graphics.SmoothingMode = SmoothingMode.HighQuality;
                            graphics.Clear(Color.Black);
                            graphics.DrawImage(image, new Rectangle(0, 0, num4, num5), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
                        }
                        using (Image image3 = new Bitmap(intThumbWidth, intThumbHeight))
                        {
                            using (Graphics graphics2 = Graphics.FromImage(image3))
                            {
                                graphics2.InterpolationMode = InterpolationMode.High;
                                graphics2.SmoothingMode = SmoothingMode.HighQuality;
                                graphics2.Clear(Color.Black);
                                int srcX = (num4 - intThumbWidth) / 2;
                                int srcY = (num5 - intThumbHeight) / 2;
                                graphics2.DrawImage(image2, new Rectangle(0, 0, intThumbWidth, intThumbHeight), srcX, srcY, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
                                graphics2.Dispose();
                                image3.Save(filename, ImageFormat.Jpeg);
                            }
                            return str;
                        }
                    }
                }
            }
            catch
            {
                File.Delete(HttpContext.Current.Server.MapPath(sSavePath + str2));
                return "图片格式不正确";
            }
            return str;
        }
    }
}

